package com.zdxlz.fcmp.business.controller.local;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdxlz.fcmp.business.entity.DeviceInfoEntity;
import com.zdxlz.fcmp.business.service.IDeviceInfoService;
import com.zdxlz.fcmp.common.core.util.R;
import lombok.AllArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zdxlz.fcmp.common.core.constant.CommonConstants.OPERATION_SUCCESS;
import static com.zdxlz.fcmp.common.core.constant.enums.ResCode.ERR_COMMON_PARAMS;

/**
 * <p>
 * 设备信息表 前端控制器
 * </p>
 *
 * @author: aman
 * @since 2025-01-30
 */
@RestController
@RequestMapping("/api/v1/device")
@AllArgsConstructor
public class DeviceInfoController {

    private IDeviceInfoService deviceInfoService;

    /**
     * @description 分页查询设备信息列表
     * @author aman
     * @date 2025/2/11 10:33
     */
    @GetMapping
    public R getList(@ParameterObject Page<DeviceInfoEntity> page, @ParameterObject DeviceInfoEntity deviceInfoEntity) {
        return R.ok(deviceInfoService.getList(page, deviceInfoEntity), OPERATION_SUCCESS);
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        return R.ok(deviceInfoService.getById(id), OPERATION_SUCCESS);
    }

    @GetMapping("/mac/{mac}")
    public R getById(@PathVariable String mac) {
        if (StrUtil.isBlank(mac)) {
            return R.failed(ERR_COMMON_PARAMS.getReason());
        }
        LambdaQueryWrapper<DeviceInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeviceInfoEntity::getMac, mac);
        return R.ok(deviceInfoService.getOne(queryWrapper), OPERATION_SUCCESS);
    }

    @PostMapping
    public boolean create(@RequestBody @Validated DeviceInfoEntity entity) {
        return deviceInfoService.save(entity);
    }

    @PutMapping("/{id}")
    public boolean update(@PathVariable Integer id, @RequestBody @Validated DeviceInfoEntity entity) {
        entity.setId(id);
        return deviceInfoService.updateById(entity);
    }

    @DeleteMapping()
    public boolean delete(@RequestBody List<Integer> ids) {
        return deviceInfoService.removeBatchByIds(ids);
    }

}
